//
// Created by yuping.li on 10/17/24.
//

#ifndef TIME_HPP_
#define TIME_HPP_

#include "rtc.h"
#include "log.hpp"

class Time {
public:
  static Time& instance() {
    static Time time{};
    return time;
  }

  void init(RTC_HandleTypeDef* rtc) {
    m_rtc = rtc;
    factor = 1.0f/static_cast<float>(m_rtc->Instance->PRLL+1)*1000;
  }

  inline uint32_t get_timestamp_ms() {
    return (m_rtc->Instance->CNTH << 16U | m_rtc->Instance->CNTL)* 1000 + (uint32_t)((m_rtc->Instance->PRLL-m_rtc->Instance->DIVL)*factor);
  }

protected:

  Time() = default;
  float factor{0.0f};
  RTC_HandleTypeDef* m_rtc;
};

#endif //TIME_HPP_
